package cn.icyzx.service;

import cn.icyzx.dao.DaoUser;
import cn.icyzx.exception.*;
import cn.icyzx.models.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.icyzx.util.StaticConstUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zk on 2016/10/6.
 */
@Service
public class UserService {

    private static Logger logger = Logger.getLogger(UserService.class);

    @Autowired
    DaoUser daoUser;

    public void updateSessionUser(HttpSession session) {
        User user = (User) session.getAttribute(StaticConstUtil.LOGIN_SIGN);
        user = getUserByUid(user.getUid());
        session.setAttribute(StaticConstUtil.LOGIN_SIGN, user);
    }

    /**
     * 修改学院，专业，班级等信息
     *
     * @param uid
     * @param college
     * @param major
     * @param classes
     */
    public void updateCollege(String uid, String college, String major, String classes) {
        try {
            if (uid == null)
                throw new TipException("请先登录");
            daoUser.updateCollege(uid, college, major, classes);
        } catch (ModifyUserInfoException e) {
            logger.error("修改学院等信息错误" + e.getMessage() + "不存在该用户，或空指针异常");
            throw new TipException("修改院系信息错误");
        }
    }

    /*** 修改昵称，签名，favorite
     *
     * @param user
     */
    public void updateBaseInfo(User user) {
        try {
            daoUser.updateBaseInfo(user);
        } catch (ModifyUserInfoException e) {
            logger.error("修改用户基本信息错误" + e.getMessage());
        }
    }

    public boolean checkPasswordByUid(String uid, String password) {
        try {
            User user = daoUser.getUserByUid(uid);
            if (password.equals(user.getPassword()))
                return true;
            else
                return false;
        } catch (GetUserException e) {
            logger.error("找不到该用户" + e.getMessage());
            return false;
        }

    }

    /**** 激活用户操作
     *
     * @param uid
     */
    public void activeUser(String uid) {
        try {
            daoUser.activeUser(uid);
        } catch (UserOperatorException e) {
            logger.error(e.getMessage() + ":不存在该用户");
        }
    }

    public void updateUserAvatar(String avatarImg, String uid) {
        try {
            daoUser.updateUserAvatar(avatarImg, uid);
        } catch (UserOperatorException e) {
            logger.error(e.getMessage());
            throw new TipException("修改头像失败");
        }
    }

    public User getUserByUserName(String username) {
        return daoUser.getUserByUsername(username);
    }

    public User getUserByUid(String uid) {
        try {
            return daoUser.getUserByUid(uid);
        } catch (GetUserException e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    public User getUserByEmail(String email) {
        try {
            return daoUser.getUserByEmail(email);
        } catch (GetUserException e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    /**
     * 学院列表
     *
     * @return
     */
    public List<String> collegeList() {
        return daoUser.collegeList();
    }

    /**
     * 专业列表
     *
     * @param college
     * @return
     */
    public List<String> majorList(String college) {
        return daoUser.majorList(college);
    }

    /**
     * 班级列表
     *
     * @return
     */
    public List<String> classesList() {
        return daoUser.classesList();
    }


}
